package main

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"server/controller"
	_ "server/docs"
	"server/middleware"
)

func CollectRoute(r *gin.Engine) *gin.Engine {
	r.Use(middleware.CORSMiddleware(), middleware.LoggerToFile())
	r.POST("/user/register", controller.Register)
	r.POST("/user/login", controller.Login)
	r.GET("/user/info", middleware.AuthMiddleware(), controller.Info)
	r.GET("/user/likes", middleware.AuthMiddleware(), controller.ShowMyLikes)
	r.PUT("/user/signature/:id", middleware.AuthMiddleware(), controller.UpdateUserSignature)

	categoryRoutes := r.Group("/categories")
	categoryController := controller.NewCategoryController()
	categoryRoutes.POST("", categoryController.Create)
	categoryRoutes.PUT("/:id", categoryController.Update)
	categoryRoutes.GET("/:id", categoryController.Show)
	categoryRoutes.DELETE("/:id", categoryController.Delete)
	categoryRoutes.GET("/list", categoryController.CategoryList)

	postRoutes := r.Group("/posts")
	postRoutes.Use(middleware.AuthMiddleware(), middleware.CORSMiddleware())
	postController := controller.NewPostController()
	postRoutes.POST("", postController.Create)
	postRoutes.PUT("/:id", postController.Update)
	postRoutes.DELETE("/:id", postController.Delete)
	postRoutes.POST("/like/:id", postController.IncreaseLikes)

	url := ginSwagger.URL("http://localhost:8080/swagger/doc.json")
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
	r.GET("/myArticles", middleware.AuthMiddleware(), postController.ShowMyArticles)
	r.GET("posts/:id", postController.Show)
	r.POST("/page/list", postController.PageList)
	r.POST("/column/:name", postController.Column)
	r.POST("/search/:title", postController.SearchArticles)

	return r
}
